﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using frag_boardgame;
using Website.BoardWebControl;

namespace Website
{
	public partial class Board : System.Web.UI.UserControl
	{
       
        private int cols;

        int boardsizeX = 399;
        int boardsizeY = 300;
        
        public int Cols
        {
            get { return cols; }
            set
            {
                if (value>0)
                    cols = value;
            }
        }
        private int rows;

        public int Rows
        {
            get { return rows; }
            set 
            {
                if (value>0)
                 rows = value; 
            }
        }

        public delegate void squareSelect(Square sqr);
        public event squareSelect OnSquareSelect;

        public HorizontalAlign Align
        {
            //get
            //{ return tblBoard.HorizontalAlign; }
            //set { tblBoard.HorizontalAlign = value; }
            get { return HorizontalAlign.Center; }
            set {}
        }
        public void AddPlayer(int ID, Pos pos)
        {
            //tblBoard.Rows[pos.row].Cells[pos.col].Text = ID.ToString();
        }

        private Panel createWallPanel(int x, int y, int width, int height)
        {
            Panel wall = new Panel();


            wall.Style.Add("position", "absolute");
            wall.Style.Add("left", x.ToString() + "px");
            wall.Style.Add("top", y.ToString() + "px");
            wall.Style.Add("width", width.ToString() + "px");
            wall.Style.Add("height", height.ToString() + "px");
            
            wall.Style.Add("background-color", "Orange");
            wall.Style.Add("z-index", "100");

            return wall;
        }


        public void DrawBoard(frag_boardgame.Board board)
        {
            int tilesize = Math.Min(boardsizeX / cols, boardsizeY / rows);

            TilesPanel.Controls.Clear();

            TilesPanel.Width = tilesize * cols;
            TilesPanel.Height = tilesize * rows;

            TilesPanel.Style.Add("position", "relative");
            for (int r = 0; r < Rows; r++)
            {
                //Panel panelr = new Panel();
                //panelr.Height = tilesize;
                for (int c = 0; c < Cols; c++)
                {

                    Square sqr = board.GetSquare(r, c);
                    TileImageButton img = new TileImageButton(sqr, tilesize);
                    
                    int left = c * tilesize;
                    int top = r * tilesize;
                    img.Click += new ImageClickEventHandler(img_Click);
                    img.Style.Add("position", "absolute");
                    img.Style.Add("left", left.ToString() + "px");
                    img.Style.Add("top", top.ToString() + "px");
                    img.Style.Add("z-index", "90");
            
                    //panelr.Controls.Add(img);
                    TilesPanel.Controls.Add(img);

                    int wallThickness = Math.Max(tilesize / 15, 2);
                    int dt = wallThickness / 2;
                    if (sqr.HasWall(SquareWalls.Down))
                    {
                        TilesPanel.Controls.Add(createWallPanel(left, top - dt, tilesize, wallThickness));
                    }
                    if (sqr.HasWall(SquareWalls.Up))
                    {
                        TilesPanel.Controls.Add(createWallPanel(left, top + tilesize - dt, tilesize, wallThickness));
                    }
                    if (sqr.HasWall(SquareWalls.Left))
                    {
                        TilesPanel.Controls.Add(createWallPanel(left - dt, top, wallThickness, tilesize));
                    }
                    if (sqr.HasWall(SquareWalls.Right))
                    {
                        TilesPanel.Controls.Add(createWallPanel(left + tilesize - dt, top, wallThickness, tilesize));
                    }



                }
                //TilesPanel.Controls.Add(panelr);
            }         


        }

        void img_Click(object sender, ImageClickEventArgs e)
        {
            TileImageButton img = (TileImageButton)sender;

            this.OnSquareSelect(img.sqr);
        }

        public frag_boardgame.Board GameBoard
        {
            set
            {
                DrawBoard(value);
            }
        }



        
        
    }

    
}